package com.suray.system.run.data.mysql.device.charge;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.system.run.data.mysql.device.entity.ChargeInfoDB;
import com.suray.system.run.data.mysql.device.entity.OffLineDesDB;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ChargeInfoDBServiceImpl extends ServiceImpl<ChargeInfoDBMapper, ChargeInfoDB> implements ChargeInfoDBService {

    @Resource
    ChargeInfoDBMapper chargeInfoDBMapper;

    @Override
    public ChargeInfoDB findChargeInfoDBBySeries(String series) {
        LambdaQueryWrapper<ChargeInfoDB> eq = new QueryWrapper<ChargeInfoDB>().lambda()
                .eq(ChargeInfoDB::getSeries, series)
                .isNull(ChargeInfoDB::getChargeEndTime);

        List<ChargeInfoDB> chargeInfoDBS = this.list(eq);
        if (chargeInfoDBS.size() == 0) {
            return null;
        }
        return chargeInfoDBS.get(0);
    }

    public List<ChargeInfoDB> getChareInfoList(String series, Date startTime, Date endTime) {
        QueryWrapper<ChargeInfoDB> eq = new QueryWrapper<>();

        eq.lambda().eq(ChargeInfoDB::getSeries, series);

        if(startTime != null){
            eq.lambda().ge(ChargeInfoDB::getChargeStartTime, startTime);
        }
        if(endTime != null){
            eq.lambda().le(ChargeInfoDB::getChargeEndTime, endTime);
        }

        return this.list(eq);
    }
}
